package com.itstyle.seckill.common.bit;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author lifujun
 * @description: 二叉树
 * @date 2024/3/20 14:06
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BitTreeNode {
    private BitTreeNode left;
    private BitTreeNode right;
    private String data;

    @Override
    public String toString() {
        return this.data;
    }


    public static void visit(BitTreeNode node){
        System.out.println("树结点的值:"+node);
    }

    // 先序遍历
    public static void preOrder(BitTreeNode node){
        if(node!=null){
            visit(node);
            preOrder(node.left);
            preOrder(node.right);
        }
    }

    // 中序遍历
    public static void inOrder(BitTreeNode node){
        if (node!=null){
            inOrder(node.left);
            visit(node);
            inOrder(node.right);
        }
    }

    public BitTreeNode(String data){
        this.data = data;
    }


    // 后序遍历
    public static void postOrder(BitTreeNode node){
        if (node!=null){
            postOrder(node.left);
            postOrder(node.right);
            visit(node);
        }
    }

    public static void main(String[] args) {
        BitTreeNode A = new BitTreeNode("20");
        BitTreeNode B = new BitTreeNode("10");
        BitTreeNode C = new BitTreeNode("30");
        BitTreeNode D = new BitTreeNode("5");
        BitTreeNode E = new BitTreeNode("15");
        BitTreeNode F = new BitTreeNode("35");
        BitTreeNode G = new BitTreeNode("11");
        A.setLeft(B);
        A.setRight(C);
        B.setLeft(D);
        B.setRight(E);
        E.setLeft(G);
        C.setRight(F);
//        preOrder(A);
        inOrder(A);
//        postOrder(A);

    }
}


